package trunk.component;

import java.awt.*;

/**
 * a line with a arrow in its head
 * Created by IntelliJ IDEA.
 * User: yipeipei
 * Date: 11/28/11
 * Time: 10:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class ArrowLine extends Line implements StatusAlterable {
    private static int ARROW_SIZE = 6;

    protected int auxiliary_X1; //auxiliary point to paint an arrowLine
    protected int auxiliary_Y1;
    protected int auxiliary_X2;
    protected int auxiliary_Y2;

    public ArrowLine(int xStart, int yStart, int xEnd, int yEnd) {
        super(xStart, yStart, xEnd, yEnd);
        initializeAuxiliaryPoint();
    }

    public ArrowLine(int xStart, int yStart, Direction direction, int length) {
        super(xStart, yStart, direction, length);
        initializeAuxiliaryPoint();
    }

    public ArrowLine(int xStart, int yStart, int xEnd, int yEnd, Status status) {
        super(xStart, yStart, xEnd, yEnd, status);
        initializeAuxiliaryPoint();
    }

    private void initializeAuxiliaryPoint() {
        if (xStart == xEnd) {
            if (yStart < yEnd) {
                this.auxiliary_X1 = xEnd - ARROW_SIZE;
                this.auxiliary_Y1 = yEnd - ARROW_SIZE;
                this.auxiliary_X2 = xEnd + ARROW_SIZE;
                this.auxiliary_Y2 = yEnd - ARROW_SIZE;
            } else if (yStart > yEnd) {
                this.auxiliary_X1 = xEnd - ARROW_SIZE;
                this.auxiliary_Y1 = yEnd + ARROW_SIZE;
                this.auxiliary_X2 = xEnd + ARROW_SIZE;
                this.auxiliary_Y2 = yEnd + ARROW_SIZE;
            }
        } else if (yStart == yEnd) {
            if (xStart < xEnd) {
                this.auxiliary_X1 = xEnd - ARROW_SIZE;
                this.auxiliary_Y1 = yEnd - ARROW_SIZE;
                this.auxiliary_X2 = xEnd - ARROW_SIZE;
                this.auxiliary_Y2 = yEnd + ARROW_SIZE;
            } else if (xStart > xEnd) {
                this.auxiliary_X1 = xEnd + ARROW_SIZE;
                this.auxiliary_Y1 = yEnd - ARROW_SIZE;
                this.auxiliary_X2 = xEnd + ARROW_SIZE;
                this.auxiliary_Y2 = yEnd + ARROW_SIZE;
            }
        }
    }

    @Override
    public void paint(Graphics g) {
//        setStatus(Status.ACTIVE);
        super.paint(g);

        //set color
        if (status == Status.NORMAL) {
            g.setColor(Color.black);
        } else if (status == Status.ACTIVE) {
            g.setColor(Color.red);
        }

        //paint
        g.drawLine(auxiliary_X1, auxiliary_Y1, xEnd, yEnd);
        g.drawLine(auxiliary_X2, auxiliary_Y2, xEnd, yEnd);
        //restore color
        g.setColor(Color.black);
    }
}
